package fnord.network.client;

import java.io.IOException;
import java.util.Collection;

import fnord.exception.BadPortException;
import fnord.illuminati.Board;
import fnord.illuminati.card.Connection;
import fnord.illuminati.card.GroupCard;
import fnord.illuminati.order.*;
import fnord.jovian.TheGUICommunicator;
import fnord.network.OrderType;
import fnord.network.server.Player;

public class Client {

	private ClientCommunicatorThread _thread;
	private TheGUICommunicator _guiCom;
	
	public Client(String host, int port, TheGUICommunicator guiCom) throws BadPortException, IOException {
		_guiCom = guiCom;
		_thread = new ClientCommunicatorThread(this, port, host);
		_thread.start();
//		System.out.println("I'm in your server.");
	}
	
	public void sendAttackOrder(Player from, GroupCard fromCard, GroupCard toCard, AttackType at, int tPower, Collection<GroupCard> tPowerCards, Connection conTo) throws IOException {
		AttackOrder ao = new AttackOrder(from, fromCard, toCard, at, tPower, tPowerCards, conTo);
		_thread.sendData(OrderType.ATTACK, ao);
	}
	
	public void sendMoneyTransferOrder(Player from, GroupCard fromCard, GroupCard toCard, int amt) throws IOException {
		MoneyTransferOrder mto = new MoneyTransferOrder(from, fromCard, toCard, amt);
		_thread.sendData(OrderType.TRANSFER_MONEY, mto);
	}
	
	public void sendTreeTransferOrder(Player from, GroupCard fromCard, GroupCard toCard, Connection conTo) throws IOException {
		TreeTransferOrder tto = new TreeTransferOrder(from, fromCard, toCard, conTo);
		_thread.sendData(OrderType.TRANSFER_CARD, tto);
	}
	
	public void sendSpendMoneyOrder(Player from, GroupCard fromCard, GroupCard towards, int amt) throws IOException {
		SpendMoneyOrder smo = new SpendMoneyOrder(from, fromCard, towards, amt);
		_thread.sendData(OrderType.SPEND_MONEY, smo);
	}
	
	public void sendChat(Player p, String mess) throws IOException {
		_thread.sendData(OrderType.CHAT, p.getChatHandle()+ mess);
	}

	public void updateGameState(Board newBoard) {
		_guiCom.takeBoard(newBoard);	
	}
	
	public void updateChat(String message) {
		_guiCom.takeChat(message);
	}
	
	public Player myPlayer() {
		return _thread.getMyPlayer();
	}
	
	public void pass(Player p) throws IOException {
		_thread.sendData(OrderType.PASS, new PassOrder(p));
	}
	
}
